import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:kxy_flutter_app/auth.dart';
import 'package:kxy_flutter_app/helper/http.dart';

class CorrectQuesView extends StatefulWidget {
  final String quesId;
  const CorrectQuesView({
    Key key,
    @required this.quesId,
  }) : super(key: key);
  @override
  State<StatefulWidget> createState() {
    return _CorrectQuesViewState();
  }
}

class _CorrectQuesViewState extends State<CorrectQuesView> {
  TextEditingController descCtrl = new TextEditingController();
  int typeIndex = 0;
  List<String> types = ['题干错误', '看不懂题', '题目错误', '图片看不清'];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('纠错')),
      backgroundColor: Colors.white,
      body: ListView(
        padding: EdgeInsets.symmetric(vertical: 20, horizontal: 15),
        children: [
          typeWidget(),
          detailWidget(),
          submitBtn(),
        ],
      ),
    );
  }

  Widget typeWidget() {
    List<Widget> btnList = new List<Widget>();
    for (var i = 0; i < types.length; i++) {
      btnList.add(typeItem(i));
    }

    return Column(
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Container(
          alignment: Alignment.centerLeft,
          padding: EdgeInsets.only(bottom: 10),
          child: Text(
            '报错类型',
            style: TextStyle(
              fontSize: 16,
              color: Colors.black87,
              fontWeight: FontWeight.bold,
            ),
          ),
        ),
        Wrap(
          alignment: WrapAlignment.start,
          spacing: 10,
          runSpacing: 10,
          children: btnList,
        ),
      ],
    );
  }

  Widget typeItem(int index) {
    var color = index == typeIndex ? Theme.of(context).primaryColor : Color.fromRGBO(240, 240, 240, 1);
    var textColor = index == typeIndex ? Colors.white : Colors.black87;
    return FlatButton(
      padding: EdgeInsets.symmetric(horizontal: 10, vertical: 7),
      color: color,
      textColor: textColor,
      onPressed: () {
        setState(() {
          typeIndex = index;
        });
      },
      child: Text(
        types[index],
        style: TextStyle(fontSize: 12),
      ),
    );
  }

  Widget detailWidget() {
    return Column(
      children: [
        Container(
          alignment: Alignment.centerLeft,
          margin: EdgeInsets.only(top: 20),
          padding: EdgeInsets.only(bottom: 10),
          child: Text(
            '备注',
            style: TextStyle(
              fontSize: 16,
              color: Colors.black87,
              fontWeight: FontWeight.bold,
            ),
          ),
        ),
        TextField(
          controller: descCtrl,
          minLines: 5,
          maxLines: 8,
          cursorWidth: 1.2,
          decoration: InputDecoration(
            hintText: '您的耐心指点，是我们前进的动力！',
            contentPadding: EdgeInsets.symmetric(vertical: 10, horizontal: 10),
            border: OutlineInputBorder(
              borderSide: BorderSide(
                width: 1.2,
                color: Colors.black54,
              ),
            ),
            focusedBorder: OutlineInputBorder(
              borderSide: BorderSide(
                width: 1.2,
                color: Theme.of(context).primaryColor,
              ),
            ),
          ),
        ),
      ],
    );
  }

  Widget submitBtn() {
    return Container(
      margin: EdgeInsets.only(top: 20),
      padding: EdgeInsets.symmetric(horizontal: 20),
      width: double.infinity,
      height: 40,
      child: ClipRRect(
        borderRadius: BorderRadius.circular(50),
        child: FlatButton(
          color: Theme.of(context).primaryColor,
          textColor: Colors.white,
          child: Text('提交'),
          onPressed: submit,
        ),
      ),
    );
  }

  Future submit() async {
    var msg = types[typeIndex] + ',' + descCtrl.value.text;
    var res = await Http.instance.post('/exam-app/correction/submit', params: {
      'quesId': widget.quesId,
      'userId': Auth.instance.userId,
      'feedback': msg,
    });
    if (res.code == 200) {
      Navigator.of(context).pop();
    } else {
      Fluttertoast.showToast(msg: res.msg);
    }
  }
}
